using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Yoozoo.UI.YGUI;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx
{
    [Serializable]
    public class YListAdaptConfig
    {
        public YList.Direction direction;
        public RectOffset padding;
        public float spacing;
        public TextAnchor childAlignment;
        public bool childControlWidth;
        public bool childControlHeight;
        public bool childScaleWidth;
        public bool childScaleHeight;
        public bool childForceExpandWidth;
        public bool childForceExpandHeight;
        
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            YListAdaptConfig other = obj as YListAdaptConfig;
            if (direction != other.direction)
            {
                return false;
            }
            
            if (padding != other.padding)
            {
                return false;
            }
            
            if (Math.Abs(spacing - other.spacing) > 0.001f)
            {
                return false;
            }
            
            if (childAlignment != other.childAlignment)
            {
                return false;
            }
            
            if (childControlWidth != other.childControlWidth)
            {
                return false;
            }
            
            if (childControlHeight != other.childControlHeight)
            {
                return false;
            }
            
            if (childScaleWidth != other.childScaleWidth)
            {
                return false;
            }
            
            if (childScaleHeight != other.childScaleHeight)
            {
                return false;
            }
            
            if (childForceExpandWidth != other.childForceExpandWidth)
            {
                return false;
            }
            
            if (childForceExpandHeight != other.childForceExpandHeight)
            {
                return false;
            }

            return true;
        }
        
        public override int GetHashCode()
        {
            int hash = 17;
            hash = hash * 23 + direction.GetHashCode();
            hash = hash * 23 + padding.GetHashCode();
            hash = hash * 23 + spacing.GetHashCode();
            hash = hash * 23 + childAlignment.GetHashCode();
            hash = hash * 23 + childControlWidth.GetHashCode();
            hash = hash * 23 + childControlHeight.GetHashCode();
            hash = hash * 23 + childScaleWidth.GetHashCode();
            hash = hash * 23 + childScaleHeight.GetHashCode();
            hash = hash * 23 + childForceExpandWidth.GetHashCode();
            hash = hash * 23 + childForceExpandHeight.GetHashCode();
            return hash;
        }
    }

    [DisallowMultipleComponent]
    public class YListAdapter : AdaptBase<YListAdaptConfig, YList>
    {
        protected override void CopyProperty(YListAdaptConfig config)
        {
            base.CopyProperty(config);
            config.direction = mComponent.direction;
            var layoutGroup = mComponent.GetComponent<HorizontalOrVerticalLayoutGroup>();
            if (layoutGroup == null)
            {
                return;
            }
            config.padding = layoutGroup.padding;
            config.spacing = layoutGroup.spacing;
            config.childAlignment = layoutGroup.childAlignment;
            config.childControlWidth = layoutGroup.childControlWidth;
            config.childControlHeight = layoutGroup.childControlHeight;
            config.childScaleWidth = layoutGroup.childScaleWidth;
            config.childScaleHeight = layoutGroup.childScaleHeight;
            config.childForceExpandWidth = layoutGroup.childForceExpandWidth;
            config.childForceExpandHeight = layoutGroup.childForceExpandHeight;
        }
        
        protected override void ApplyConfig(YListAdaptConfig config)
        {
            base.ApplyConfig(config);
            mComponent.direction = config.direction;  
            var layoutGroup = mComponent.GetComponent<HorizontalOrVerticalLayoutGroup>();
            if (layoutGroup == null)
            {
                return;
            }
            layoutGroup.padding = config.padding;
            layoutGroup.spacing = config.spacing;
            layoutGroup.childAlignment = config.childAlignment;
            layoutGroup.childControlWidth = config.childControlWidth;
            layoutGroup.childControlHeight = config.childControlHeight;
            layoutGroup.childScaleWidth = config.childScaleWidth;
            layoutGroup.childScaleHeight = config.childScaleHeight;
            layoutGroup.childForceExpandWidth = config.childForceExpandWidth;
            layoutGroup.childForceExpandHeight = config.childForceExpandHeight;
            
            if (config.direction == YList.Direction.Horizontal)
            {
                layoutGroup.CalculateLayoutInputHorizontal();
            }
            else
            {
                layoutGroup.CalculateLayoutInputVertical();
            }
        }
    }
}
